<?php

class Application_Model_RelatorioContasReceber extends Zend_Db_Table_Abstract {

    protected $_name = 'relatorio';
    protected $_primary = 'codigo';
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_RelatorioContasReceber();
            return self::$instance;
        }
        return self::$instance;
    }

    public function lista($data) {

        //print_r($data); die;

        $sql = 'SELECT c.descricao as conta, c.dt_faturamento as faturamento, a.dt_atendimento as atendimento, a.observacao as atobservation, s.codigo, s.descricao as segdesc, p.nome as cliente, at.codigo, at.descricao as atividade,
                    f.nome as funcionario, f.codigo, p.codigo
                    FROM contas_receber c

                    LEFT JOIN atendimento as a on a.codigo = c.atendimento
                    JOIN segmento as s on s.codigo = a.segmento
                    JOIN atividade as at on at.codigo = a.atividade
                    JOIN pessoa as f on f.codigo = a.funcionario
                    JOIN pessoa as p on p.codigo = a.paciente

                    WHERE c.dt_exclusao is null';

        if(isset($data['descricao']) && !empty($data['descricao'])){
            $sql.=' and c.descricao like \''. $data['descricao']. '%\'';
        }

        if(isset($params["dt_inicio"]) && !empty($params["dt_inicio"]) && isset($params["dt_fim"]) && !empty($params["dt_fim"])){
                $sql.=' and dt_faturamento between \''.Util_Function_Util::formataData($params["dt_inicio"], 'Y/M/d').'\' and \''.Util_Function_Util::formataData($params["dt_fim"], 'Y/M/d').'\'';
            } else if(isset($params["dt_inicio"]) && !empty($params["dt_inicio"]) && isset($params["dt_fim"]) && empty($params["dt_fim"])){
                $sql.=' and dt_faturamento = \''.Util_Function_Util::formataData($params["dt_inicio"], 'Y/M/d').'\'';
            } else if(isset($params["dt_inicio"]) && empty($params["dt_inicio"]) && isset($params["dt_fim"]) && !empty($params["dt_fim"])){
                $sql.=' and dt_faturamento = \''.Util_Function_Util::formataData($params["dt_fim"], 'Y/M/d').'\'';
            }

        if(isset($data['atividadePaciente']) && !empty($data['atividadePaciente'])){
            $sql.=' and at.codigo = \''. $data['atividadePaciente'].'\'';
        }

        if(isset($data['segmentoPaciente']) && !empty($data['segmentoPaciente'])){
            $sql.=' and s.codigo = \''. $data['segmentoPaciente'].'\'';
        }

        if(isset($data['funcionario']) && !empty($data['funcionario'])){
            $sql.=' and f.codigo = \''. $data['funcionario'].'\'';
        }

        if(isset($data['cliente']) && !empty($data['cliente'])){
            $sql.=' and p.codigo = \''. $data['cliente'].'\'';
        }
        /*if (isset($orderName) && isset($order)) {
            $sql .= ' order by d.' . $orderName . ' ' . $order;
        }*/

        $rs = $this->getAdapter()->query($sql)->fetchAll();

        return $rs;
    }

    
}